package com.whyx.project.openapi.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;

@Configuration
public class RedisConfig {
    @Value("${RealTimeInfo.redis.host}")
    private String host;

    @Value("${RealTimeInfo.redis.port}")
    private int port;

    @Value("${RealTimeInfo.redis.timeout}")
    private int timeout;

    @Value("${RealTimeInfo.redis.password}")
    private String password;

    @Value("${RealTimeInfo.redis.database}")
    private int database;

    @Value("${RealTimeInfo.redis.pool.max-idle}")
    private int maxIdle;

    @Value("${RealTimeInfo.redis.pool.min-idle}")
    private int minIdle;

    @Value("${RealTimeInfo.redis.pool.max-wait}")
    private int maxWaitMillis;

    private RedisTemplate<String, String> stringRedisTemplate;
    public RedisTemplate<String, String> redisTemplate() {
        if (stringRedisTemplate == null) {
            RedisConnectionFactory factory = redisConnectionFactory();

            stringRedisTemplate = new RedisTemplate<String, String>();
            stringRedisTemplate.setConnectionFactory(factory);
            //key序列化方式;但是如果方法上有Long等非String类型的话，会报类型转换错误；

            RedisSerializer<String> redisSerializer = new StringRedisSerializer();//Long类型不可以会出现异常信息;
            stringRedisTemplate.setKeySerializer(redisSerializer);
            stringRedisTemplate.setHashKeySerializer(redisSerializer);
            stringRedisTemplate.setValueSerializer(redisSerializer);
            stringRedisTemplate.afterPropertiesSet();
        }
        return stringRedisTemplate;
//        RedisConnectionFactory factory = redisConnectionFactory();
//
//        RedisTemplate<String, String> redisTemplate = new RedisTemplate<String, String>();
//        redisTemplate.setConnectionFactory(factory);
//        //key序列化方式;但是如果方法上有Long等非String类型的话，会报类型转换错误；
//
//        RedisSerializer<String> redisSerializer = new StringRedisSerializer();//Long类型不可以会出现异常信息;
//        redisTemplate.setKeySerializer(redisSerializer);
//        redisTemplate.setHashKeySerializer(redisSerializer);
//        redisTemplate.setValueSerializer(redisSerializer);
//        redisTemplate.afterPropertiesSet();
//        return redisTemplate;
    }

    @Primary
    @Bean(name = "Factory")
    public JedisConnectionFactory redisConnectionFactory() {
        JedisPoolConfig jedisPoolConfig = jedisPoolConfig();
        JedisConnectionFactory factory = new JedisConnectionFactory();
        factory.setHostName(host);
        factory.setPort(port);
        factory.setPassword(password);
        //存储的库
        factory.setDatabase(database);
        //设置连接超时时间
        factory.setTimeout(timeout);
        factory.setUsePool(true);
        factory.setPoolConfig(jedisPoolConfig);
        return factory;
    }


    public JedisPoolConfig jedisPoolConfig() {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxIdle(maxIdle);
        jedisPoolConfig.setMinIdle(minIdle);
        jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
        return jedisPoolConfig;
    }
}
